import _ from "lodash";

/**
 *
 * @param oldVal memo 第一参数
 * @param newVal memo 第二参数
 * @param key?  key不传 默认深层次比较  传单个 单个比较 传多个是以数组字符串形式
 * @returns  bool
 */

export const memoCallBack = (oldVal: any, newVal: any, key = null) => {
    if (_.isNull(key)) {
        return _.isEqual(oldVal, newVal);
    } else if (_.isString(key)) {
        return _.isEqual(oldVal[key], newVal(key));
    } else if (_.isArray(key)) {
        const oldObj: any = {};
        const newObj: any = {};
        _.forEach(key, (val: string) => {
            oldObj[val] = oldVal[val];
            newObj[val] = oldVal[val];
        });
        return _.isEqual(oldObj, newObj);
    }
};

/**
 *
 * @param txt 需要复制的文案
 */

export const handleCopy = txt => {
    const textField = document.createElement("textarea");
    textField.innerText = txt;
    document.body.appendChild(textField);
    textField.select();
    document.execCommand("copy");
    textField.remove();
};
